module Main where

import System.Environment (getArgs)
import Text.Parsec (runParser)
import qualified Data.Map as Map

import qualified LogiLang.Parser as Parser
import LogiLang.Environment
import LogiLang.Query
import LogiLang.Pretty
import LogiLang.Instantiate

runQuery db rules query = do
	mapM putStrLn pretty
	putStrLn "---" 
	where
	frames = (qeval db rules query [[Map.empty]])
	patterns = map (instantiateQ query) frames
	pretty = map pprintQ patterns 	 

runFile filename = do
	source <- readFile filename
	let result = runParser Parser.logifile () filename source
	case result of
		Left err -> do
			putStrLn "Error at "
			print err
		Right (db, rules, queries) -> do
			mapM (runQuery db rules) queries
			return ()

	

main = do
	args <- getArgs
	mapM runFile args

